//
//  LcCellAdapter.h
//  18Birdies
//
//  Created by Lc on 15/4/2.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class LcCellData;
@class LcSelectableCellData;
@class LcCellAdapter;
@class LcCell;

typedef void (^OnCellAdapterClickedHanlder)(LcCellData* cellData);

@protocol LcCellAdapterDelegate <NSObject>
@required
- (void)onLoadMore:(LcCellAdapter*)cellAdapter;
- (void)onSelectionChanged:(NSString*)group cellAdapter:(LcCellAdapter*)cellAdapter;
- (void)onCommitEditing:(NSIndexPath*)indexPath cellAdapter:(LcCellAdapter*)cellAdapter;
@end

@interface LcCellAdapter : NSObject <UITableViewDataSource>

@property(nonatomic, weak) UITableView* tableView;
@property(nonatomic, weak) id<LcCellAdapterDelegate> delegate;
@property(nonatomic, assign) BOOL hasMore;
@property(nonatomic, assign) BOOL skipDuplicateChecking;

@property(nonatomic, strong) OnCellAdapterClickedHanlder onCellAdapterClickedHanlder;

// call when api request completes
- (void)loadMoreFinished;

// need call refreshCell/refreshAllCells after cellData changes
- (void)addCellDatas:(NSArray*)cellDatas;
- (void)addCellData:(LcCellData*)cellData;
- (void)insertCellDatas:(NSArray*)cellDatas atIndex:(int)index;
- (void)removeCellData:(LcCellData*)cellData count:(int)count;
- (void)removeAllCellDatas;
- (NSArray*)allCellDatas;
- (NSArray*)cellDatasOfClass:(Class)aClass;

// cells with group
- (LcCellData*)cellDataInGroup:(NSString*)group;
- (NSArray*)cellDatasInGroup:(NSString*)group;

// returns nil if cell is not visible or index path is out of range
- (LcCell*)cellWithCellData:(LcCellData*)cellData;
- (LcCell*)cellAtIndex:(int)index;

// cells will be refreshed after these function called
- (void)insertCellDatas:(NSArray*)cellDatas
                atIndex:(int)index
           useAnimation:(UITableViewRowAnimation)rowAnimation;
- (void)replaceCellData:(LcCellData*)cellData
           withCellData:(LcCellData*)newCellData
           useAnimation:(UITableViewRowAnimation)rowAnimation;
- (void)removeCellData:(LcCellData*)cellData
                 count:(int)count
         userAnimation:(UITableViewRowAnimation)rowAnimation;

- (id)cellDataAtIndex:(int)index;
// returns NSNotFound if not found
- (NSUInteger)indexOfCellData:(LcCellData*)cellData;

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView*)tableView
      willDisplayCell:(UITableViewCell*)cell
    forRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView*)tableView
    didEndDisplayingCell:(UITableViewCell*)cell
       forRowAtIndexPath:(NSIndexPath*)indexPath;

- (void)onRowSelectedAtIndex:(NSInteger)index;

- (void)refreshCell:(LcCellData*)cellData;
- (void)refreshCellArray:(NSArray*)cellDatas;
- (void)refreshCells:(NSString*)group;
- (void)refreshAllCells;

- (void)hide:(LcCellData*)cellData hidden:(BOOL)hidden needAutoScroll:(BOOL)needAutoScroll;
- (void)hideToLastMember:(LcCellData*)cellData hidden:(BOOL)hidden;
- (void)hideToLastMember:(LcCellData*)cellData hidden:(BOOL)hidden animated:(BOOL)animated;
- (void)hideFromMember:(LcCellData*)fromCellData
              toMember:(LcCellData*)toCellData
                hidden:(BOOL)hidden
              animated:(BOOL)animated;

// for LcSelectableCellData
- (void)selectCellData:(LcSelectableCellData*)selectableCellData
    deselectOthersInGroup:(BOOL)deselectOthersInGroup;
- (LcSelectableCellData*)selectedCellDataInGroup:(NSString*)group;
- (NSArray*)selectedCellDatasInGroup:(NSString*)group;

- (NSArray*)removeDuplicates:(NSArray*)withIdObjects;

//- (void)setDefaultCellText:(NSString*)text;
//- (void)setDefaultCellAttributeText:(NSAttributedString*)attributeText;
//- (void)setDefaultCellTopMargin:(CGFloat)topMargin;
//- (void)showDefaultCell;
//- (void)hideDefaultCell;

@end
